﻿#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#if 0
int main()
{
	int a = 10;
	int* pa = &a;//取出a的地址并存储到指针变量pa中 

	return 0;
}


int main()
{
	int a = 100;
	int* pa = &a;
	*pa = 0;
	return 0;
}


////指针变量的⼤⼩取决于地址的⼤⼩ 
//32位平台下地址是32个bit位（即4个字节） 
//64位平台下地址是64个bit位（即8个字节） 
//本代码默认是64位
int main()
{
	printf("%zd\n", sizeof(char*));
	printf("%zd\n", sizeof(short*));
	printf("%zd\n", sizeof(int*));
	printf("%zd\n", sizeof(double*));

	return 0;
}


int main()
{
	int a = 0x11223344;
	char* pa = &a;
	*pa = 0;
	return 0;
}


int main()
{
	int n = 10;
	char* pc = (char*)&n;//强转
	int* pi = &n;

	printf("n的地址=%p\n", &n);
	printf("pc的地址=%p\n", pc);
	printf("pc+1的地址=%p\n", pc + 1);
	printf("pi的地址=%p\n", pi);
	printf("pi+1的地址=%p\n", pi + 1);
	return 0;
}

int main()
{
	int m = 0;
	m = 20;//m是可以修改的 
	const int n = 0;
	n = 20;//n是不能被修改的 
	return 0;
}

int main()
{
	const int n = 0;
	printf("n = %d\n", n);
	int* p = &n;
	*p = 20;
	printf("n = %d\n", n);
	return 0;
}


void test1()
{
	int n = 10;
	int m = 20;
	int* p = &n;
	*p = 20;
	p = &m; 

}

void test2()
{
	int n = 10;
	int m = 20;
	const int* p = &n;
	*p = 20;//被限制了不能修改
	p = &m; 
}
void test3()
{
	int n = 10;
	int m = 20;
	int* const p = &n;
	*p = 20;

}

void test4()
{
	int n = 10;
	int m = 20;
	int const* const p = &n;
	*p = 20; //被限制了不能修改

	p = &m; //被限制了不能修改

}

int main()
{
	//测试⽆const修饰的情况 
	test1();
	//测试const放在*的左边情况 
	test2();
	//测试const放在*的右边情况 
	test3();
	//测试*的左右两边都有const 
	test4();
	return 0;
}


//指针+- 整数 

int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	int* p = &arr[0];
	int i = 0;
	int sz = sizeof(arr) / sizeof(arr[0]);
	for (i = 0; i < sz; i++)
	{
		printf("%d ", *(p + i));//p+i 这⾥就是指针+整数 
	}							//这里*(p + i)相当于arr[i],*(p + i)就需要给p移动地址然后解引用出来
	return 0;
}



int my_strlen(char* str1)//使用指针去接受
{
	int count = 0;
	while (*str1 != '\0')
	{
		count++;
		str1++;
	}
	return count;
}
int main()
{
	//strlen 是求字符串的长度的，统计在‘\0’之前的元素；
	char str[] = "abcdef";
	//str内容[a,b,c,d,e,f,\0];
	int len = my_strlen(str);//数组名就是数组的首地址
	printf("%d\n", len);
	return 0;
}

int my_strlen(char* s)
{
	char* p = s;//使用指针p储存s中的数组的首元素
	while (*p != '\0')
		p++;//给p进行移动
	return p - s;
}

int main()
{
	char str[] = "abcdef";
	int len = my_strlen(str);//数组名就是数组的首地址
	printf("%d\n", len);
	return 0;
}

int main()
{
	int* p;//局部变量指针未初始化，默认为随机值 
	*p = 20;
	return 0;
}

int main()
{
	int arr[10] = { 0 };
	int* p = &arr[0];
	int i = 0;
	for (i = 0; i <= 11; i++)
	{
		//当指针指向的范围超出数组arr的范围时，p就是野指针 
		*(p++) = i;
	}
	return 0;
}

int* test()
{
	int n = 100;
	return &n;
}

int main()
{
	int* p = test();
	printf("%d\n", *p);
	return 0;
}

int main()
{
	int num = 10;
	int* p1 = &num;
	int* p2 = NULL;
	return 0;
}

#define NDEBUG
#include<assert.h>
int main()
{
	int* p = NULL;
	assert(p != NULL);
}


#include<assert.h>
int my_strlen(const char* str)
{
	int count = 0;
	assert(str);//防止str为野指;
	while (*str)
	{
		count++;
		str++;
	}
	return count;
}
int main()
{
	int len = my_strlen("abcdef");
	printf("%d\n", len);
	return 0;
}


void Swap(int x, int y)
{
	int temp = 0;
		temp = x;
		x = y;
		y = temp;
}
int main()
{
	int a = 2;
	int b = 3;
	printf("交换前的值 a=%d b=%d\n", a, b);
	Swap(a, b);
	printf("交换后的值 a=%d b=%d\n", a, b);

	return 0;
}
#endif

void Swap1(int* x, int* y)//接受的为指针类型，所以使用指针变量去接受。
{
	int temp = 0;
	temp =*x;
	*x =*y;
	*y = temp;
}
int main()
{
	int a = 2;
	int b = 3;
	printf("交换前的值 a=%d b=%d\n", a, b);
	Swap1(&a,&b);//因为要使用指针所以直接传入地址即可
	printf("交换后的值 a=%d b=%d\n", a, b);

	return 0;
}